//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package corntab;

import java.util.StringTokenizer;

public class CrontabParser {
    CrontabParser() {
    }

    public static CrontabEntry marshall(String entry) throws CrontabEntryException {
        boolean[] bHours = new boolean[24];
        boolean[] bMinutes = new boolean[60];
        boolean[] bMonths = new boolean[12];
        boolean[] bDaysOfWeek = new boolean[7];
        boolean[] bDaysOfMonth = new boolean[31];
        CrontabEntry ceb = new CrontabEntry();
        StringTokenizer tokenizer = new StringTokenizer(entry);
        int numTokens = tokenizer.countTokens();

        for(int i = 0; tokenizer.hasMoreElements(); ++i) {
            String token = tokenizer.nextToken();
            switch(i) {
            case 0:
                parseToken(token, bMinutes, false);
                ceb.setBMinutes(bMinutes);
                ceb.setMinutes(token);
                break;
            case 1:
                parseToken(token, bHours, false);
                ceb.setBHours(bHours);
                ceb.setHours(token);
                break;
            case 2:
                parseToken(token, bDaysOfMonth, true);
                ceb.setBDaysOfMonth(bDaysOfMonth);
                ceb.setDaysOfMonth(token);
                break;
            case 3:
                parseToken(token, bMonths, true);
                ceb.setBMonths(bMonths);
                ceb.setMonths(token);
                break;
            case 4:
                parseToken(token, bDaysOfWeek, false);
                ceb.setBDaysOfWeek(bDaysOfWeek);
                ceb.setDaysOfWeek(token);
            }
        }

        if (numTokens < 5) {
            throw new CrontabEntryException("The number of items is < 5 at ".concat(String.valueOf(String.valueOf(entry))));
        } else {
            return ceb;
        }
    }

    public static String unmarshall(CrontabEntry ceb) throws CrontabEntryException {
        StringBuffer sb = new StringBuffer();
        sb.append(String.valueOf(String.valueOf(ceb.getMinutes())).concat(" "));
        sb.append(String.valueOf(String.valueOf(ceb.getHours())).concat(" "));
        sb.append(String.valueOf(String.valueOf(ceb.getDaysOfMonth())).concat(" "));
        sb.append(String.valueOf(String.valueOf(ceb.getMonths())).concat(" "));
        sb.append(String.valueOf(String.valueOf(ceb.getDaysOfWeek())).concat(" "));
        return sb.toString();
    }

    public static void parseToken(String token, boolean[] arrayBool, boolean bBeginInOne) throws CrontabEntryException {
        int each = 1;

        try {
            int index = token.indexOf("/");
            if (index > 0) {
                each = Integer.parseInt(token.substring(index + 1));
                token = token.substring(0, index);
            }

            if (token.equals("*")) {
                for(int i = 0; i < arrayBool.length; i += each) {
                    arrayBool[i] = true;
                }

            } else {
                index = token.indexOf(",");
                if (index > 0) {
                    StringTokenizer tokenizer = new StringTokenizer(token, ",");

                    while(tokenizer.hasMoreTokens()) {
                        parseToken(tokenizer.nextToken(), arrayBool, bBeginInOne);
                    }

                } else {
                    index = token.indexOf("-");
                    int start;
                    if (index <= 0) {
                        start = Integer.parseInt(token);
                        if (bBeginInOne) {
                            --start;
                        }

                        arrayBool[start] = true;
                    } else {
                        start = Integer.parseInt(token.substring(0, index));
                        int end = Integer.parseInt(token.substring(index + 1));
                        if (bBeginInOne) {
                            --start;
                            --end;
                        }

                        for(int j = start; j <= end; j += each) {
                            arrayBool[j] = true;
                        }

                    }
                }
            }
        } catch (Exception var9) {
            throw new CrontabEntryException("Smth was wrong with ".concat(String.valueOf(String.valueOf(token))));
        }
    }
}
